package com.kingdee.eas.custom.zyxm.app;

import java.util.Vector;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.assistant.CashFlowItemCollection;
import com.kingdee.eas.basedata.assistant.CashFlowItemFactory;
import com.kingdee.eas.basedata.assistant.CashFlowItemInfo;
import com.kingdee.eas.basedata.assistant.ICashFlowItem;
import com.kingdee.eas.basedata.org.CompanyOrgUnitFactory;
import com.kingdee.eas.basedata.org.CompanyOrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.zyxm.IXjllb;
import com.kingdee.eas.custom.zyxm.XjllbFactory;
import com.kingdee.eas.custom.zyxm.XjllbInfo;
import com.kingdee.eas.fi.gl.CashFlowDisplayRowInfo;
import com.kingdee.eas.fi.gl.CashFlowFilterPara;
import com.kingdee.eas.fi.gl.CashflowRecordFactory;
import com.kingdee.eas.fi.gl.ICashflowRecord;

public class XJLLFacadeControllerBean extends AbstractXJLLFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.zyxm.app.XJLLFacadeControllerBean");

    
    protected CompanyOrgUnitInfo getCurrentCompany(Context ctx, String companyID) throws EASBizException, BOSException
    {
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add(new SelectorItemInfo("*"));
            sic.add(new SelectorItemInfo("baseCurrency.id"));
            sic.add(new SelectorItemInfo("baseCurrency.number"));
            sic.add(new SelectorItemInfo("baseCurrency.name"));
            sic.add(new SelectorItemInfo("baseCurrency.precision"));
            sic.add(new SelectorItemInfo("reportCurrency.id"));
            sic.add(new SelectorItemInfo("reportCurrency.number"));
            sic.add(new SelectorItemInfo("reportCurrency.name"));
            sic.add(new SelectorItemInfo("reportCurrency.precision"));
            sic.add(new SelectorItemInfo("accountPeriodType.id"));
            sic.add(new SelectorItemInfo("accountPeriodType.number"));
            sic.add(new SelectorItemInfo("accountPeriodType.name"));
            sic.add(new SelectorItemInfo("accountTable.id"));
            sic.add(new SelectorItemInfo("accountTable.name"));
            sic.add(new SelectorItemInfo("accountTable.number"));
            CompanyOrgUnitInfo      company = CompanyOrgUnitFactory.getLocalInstance(ctx).getCompanyOrgUnitInfo(new ObjectUuidPK(companyID), sic);
        return company;
    }
    
	protected String _insertXJLLB(Context ctx, String companyID)throws BOSException {
		
		CompanyOrgUnitInfo companyInfo;
		try {
			companyInfo = getCurrentCompany( ctx,  companyID);
			CashFlowFilterPara   cashFlowFilterPara = new CashFlowFilterPara();
	           cashFlowFilterPara.setAccountTabel(companyInfo.getAccountTable());
	           cashFlowFilterPara.setPeriodType(companyInfo.getAccountPeriodType());
	           cashFlowFilterPara.setCurrency(companyInfo.getBaseCurrency());
	           cashFlowFilterPara.setCurrencyId(companyInfo.getBaseCurrency().getId().toString());
	           cashFlowFilterPara.setTermOrDate(true);
	           cashFlowFilterPara.setYearFrom((short)2017);
	           cashFlowFilterPara.setYearTo((short)2017);
	           cashFlowFilterPara.setMonthFrom((short)12);
	           cashFlowFilterPara.setMonthTo((short)12);
	           cashFlowFilterPara.setType((short)-1);
	           CashFlowItemCollection col = CashFlowItemFactory.getLocalInstance(ctx).getCashFlowItemCollection("select level order by level desc");
	           if(col == null || col.size() == 0){
	           }
	           else{
	        	   cashFlowFilterPara.setLevel((short)col.get(0).getLevel());
	               cashFlowFilterPara.setDisplayCashBalanceForBeginAndEnd(true);
	               ICashflowRecord iCashflowRecord = CashflowRecordFactory.getLocalInstance(ctx);
	               Object arrayObj[];
					arrayObj = iCashflowRecord.genCashFlow(cashFlowFilterPara);
				     showDataAtTable(ctx,arrayObj, companyInfo);
	           }
		} catch (EASBizException e1) {
			e1.printStackTrace();
		}
	
           
   		return super._insertXJLLB(ctx, companyID);
            
	}
    

    protected void showDataAtTable(Context ctx,Object arrayObj[],CompanyOrgUnitInfo companyInfo) throws BOSException, EASBizException
    {
       	int f=1;
    	IXjllb iXjllb=XjllbFactory.getLocalInstance(ctx);
    	ICashFlowItem iCashFlowItem=CashFlowItemFactory.getLocalInstance(ctx);
    	
        int iLength = arrayObj != null ? arrayObj.length : 0;
        for(int i = 0; i < iLength; i++)
        {
            if(arrayObj[i] instanceof String)
                continue;
            Vector vecLine = (Vector)arrayObj[i];
        	
            int sizeVec = vecLine != null ? vecLine.size() : 0;
            int j=1;
            for(int k = 0; k < sizeVec; k++)
            {
                CashFlowDisplayRowInfo row = (CashFlowDisplayRowInfo)vecLine.elementAt(k);
                CashFlowItemInfo itemInfo=  row.itemInfo;
                XjllbInfo info =new XjllbInfo();
                BOSUuid id= BOSUuid.create(info.getBOSType());
                info.setId(id);
                info.setName(id.toString());
                info.setComID(companyInfo.getId().toString());
                info.setComNum(companyInfo.getNumber());
                info.setComNam(companyInfo.getName());
                String name=row.name.trim();
                info.setXjNam(name);
                if(iCashFlowItem.exists("where name='"+name+"'")){
                    CashFlowItemInfo csInfo= (CashFlowItemInfo) iCashFlowItem.getDataBaseInfo("where name='"+name+"'");
                    if(csInfo!=null){
                    	  info.setXjNum(csInfo.getNumber());
                          info.setXjID(csInfo.getId().toString());
                          info.setLllx(csInfo.getDirection().getAlias());
                    }
                }
                info.setPeriod("201712");
                info.setAmount(row.sum);
                info.setHc(row.seq);
//                if(row.seq!=null){
//                    info.setDescription(Integer.toString(row.seq));
//                }
                info.setSeq(f);
                iXjllb.addnew(info);
                j++;
                f++;
                
            }
        }

    }
}